package com.xkdx.guangguang.fragment.my.setup;

import java.util.HashMap;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.my.setup.UpdateVersionModule.Startup;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class SetupFragment extends BaseFragment implements OnClickListener {
	LinearLayout ll_about, ll_suggest, ll_version, ll_help, ll_notify, ll_sms,ll_ip,ll_posquery;

	SharePrefenceUtil su, su_city, su_version;
	Button setup_back;
	View view;
	UpdateVersionPresistener updateVersionPresistener;
	UpdateVerisonAction updateVerisonAction;
	UpdateVersionModule updateVersionModule;
	BaseFragment baseFragment;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		baseFragment = this;
		su = new SharePrefenceUtil(getActivity(), IConstants.SP_USER);
		su_version = new SharePrefenceUtil(getActivity(), IConstants.SP_VERSION);
		su_city = new SharePrefenceUtil(getActivity(), IConstants.SP_CITY);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.setup, container, false);
		IConstants.hideInputMethodAways(getActivity(), view);
		findView();
		setOnClick();
		return view;
	}

	@Override
	public void findView() {

		ll_help = (LinearLayout) view.findViewById(R.id.setup_about_ll3);
		ll_suggest = (LinearLayout) view.findViewById(R.id.setup_about_ll1);
		ll_version = (LinearLayout) view.findViewById(R.id.setup_about_ll2);
		ll_about = (LinearLayout) view.findViewById(R.id.setup_about_ll4);
		ll_notify = (LinearLayout) view.findViewById(R.id.setup_about_ll5);
		ll_sms = (LinearLayout) view.findViewById(R.id.setup_sms);
		ll_ip = (LinearLayout)view.findViewById(R.id.setup_about_ll6);
		ll_posquery = (LinearLayout)view.findViewById(R.id.setup_about_ll7);
		setup_back = (Button) view.findViewById(R.id.setup_back);
		
		updateVerisonAction = new UpdateVerisonAction(getActivity());
		updateVersionModule = new UpdateVersionModule();

	}

	@Override
	public void setOnClick() {

		ll_about.setOnClickListener(this);
		ll_suggest.setOnClickListener(this);
		ll_version.setOnClickListener(this);
		ll_help.setOnClickListener(this);
		ll_notify.setOnClickListener(this);
		setup_back.setOnClickListener(this);
		ll_sms.setOnClickListener(this);
		ll_ip.setOnClickListener(this);
		ll_posquery.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.setup_about_ll4:
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			AboutFragment aboutFragment = new AboutFragment();
			ft.replace(R.id.fragment, aboutFragment);
			ft.addToBackStack(null);
			ft.commit();
			break;
		case R.id.setup_about_ll1:
			FragmentTransaction ft1 = getFragmentManager().beginTransaction();
			SuggestFragment suggestFragment = new SuggestFragment();
			ft1.replace(R.id.fragment, suggestFragment);
			ft1.addToBackStack(null);
			ft1.commit();

			break;
		case R.id.setup_about_ll2:
			dataLoadDialog.show();
			updateVersionPresistener = new UpdateVersionPresistener(
					baseFragment);
			updateVersionPresistener.setActitons(updateVerisonAction);
			updateVersionPresistener.setModule(updateVersionModule);
			updateVersionPresistener.execute();
			break;
		case R.id.setup_about_ll3:
			HelpFragment helpFragment = new HelpFragment();
			FragmentTransaction ft4 = getFragmentManager().beginTransaction();
			ft4.replace(R.id.fragment, helpFragment);
			ft4.addToBackStack(null);
			ft4.commit();
			break;
		case R.id.setup_about_ll5:
			NotifyFragment notifyFragment = new NotifyFragment();
			FragmentTransaction ft5 = getFragmentManager().beginTransaction();
			ft5.replace(R.id.fragment, notifyFragment);
			ft5.addToBackStack(null);
			ft5.commit();
			break;
		case R.id.setup_back:
			getFragmentManager().popBackStack();

			break;
		case R.id.setup_sms:

			Uri smsToUri = Uri.parse("smsto:");
			Intent sendIntent = new Intent(Intent.ACTION_VIEW, smsToUri);
			sendIntent.putExtra("sms_body",
					"http://dl.guangguang.net.cn/GuangGuang.apk");
			sendIntent.setType("vnd.android-dir/mms-sms");
			startActivityForResult(sendIntent, 1002);
			break;
			
		case R.id.setup_about_ll6:
			SetUpIpFragment setUpIpFragment = new SetUpIpFragment();
			FragmentTransaction ft6 = getFragmentManager().beginTransaction();
			ft6.replace(R.id.fragment, setUpIpFragment);
			ft6.addToBackStack(null);
			ft6.commit();
			break;
			
		case R.id.setup_about_ll7:
			
			SetUpPosQueryFragment setUpPostQueryFragment = new SetUpPosQueryFragment();
			FragmentTransaction ft7 = getFragmentManager().beginTransaction();
			ft7.replace(R.id.fragment, setUpPostQueryFragment);
			ft7.addToBackStack(null);
			ft7.commit();
			
			break;
		default:
			break;
		}
	}

	private void download(String clientDownloadUrl) {
		IConstants.download(getActivity(), clientDownloadUrl);
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			final Startup startup = updateVersionModule.startup;
			if (startup.getClientUpdate().equals("True")) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						getActivity());
				builder.setTitle("有新版本,是否更新");
				builder.setNegativeButton("是",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								download(startup.getClientDownloadUrl());
							}
						});
				builder.setPositiveButton("否", null);
				builder.create().show();
			} else {
				Toast.makeText(getActivity(), "已经是最新版本", Toast.LENGTH_SHORT)
						.show();
			}

		} else {
			showErrorTip(map);
		}

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoading() {
		// TODO Auto-generated method stub

	}

}
